package lesson05;

import java.sql.*;

/**
 *
 */
public class TestJDBC1 {
    public static void main(String[] args) throws SQLException {
        // 1. 加载驱动 省略 ，引入jar包自动加载了

        // 2. 创建链接
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest", "root", "root");
        System.out.println(conn.isClosed());

        // 3. 创建执行器：
        String sql = "SELECT t1.id, t1.title, t1.content, t1.cdate \n" +
                "FROM sns_blob t1 ";
        PreparedStatement ps = conn.prepareStatement(sql);

        // 4. 执行SQl,处理结果
        ResultSet rs = ps.executeQuery();
        // 处理结果集
        while (rs.next()) { // 有下一条记录并且指向这一行
            int id = rs.getInt(1);
            String title = rs.getString(2);
            String content = rs.getString("content");
            Timestamp ts = rs.getTimestamp(4);
            System.out.println("-------------------------------------------");
            System.out.println(id + "@" + title + "@" + content + "@" + ts);
        }

        // 5. 关闭资源
        conn.close();

    }
}
